#include <QDebug>
#include <QHeaderView>
#include <QShowEvent>
#include "ErrorTable.h"
#include "Include/Settings.h"
#include "TextEditor/ErrorModel.h"

ErrorTable::ErrorTable(QWidget *parent) :
    QTableView(parent),
    _height(100)
{
    verticalHeader()->hide();
    setSelectionMode(QAbstractItemView::SingleSelection);
    setSelectionBehavior(QAbstractItemView::SelectRows);
    setFont(Plugins::Settings::instance()->monoFont());
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    //horizontalHeader()->setResizeMode(0, QHeaderView::Fixed);
    //horizontalHeader()->setResizeMode(1, QHeaderView::Fixed);
    //horizontalHeader()->setResizeMode(2, QHeaderView::Fixed);
}

void ErrorTable::setModel(QAbstractItemModel *model)
{
    QTableView::setModel(model);
    resizeRowsToContents();
}

void ErrorTable::currentChanged(const QModelIndex & current, const QModelIndex & /*previous*/)
{
    emit activated(current.data(Qt::EditRole).toInt());
}

void ErrorTable::resizeEvent(QResizeEvent *event)
{
    setColumnWidth(0, 50);
    setColumnWidth(1, width() - 120);
    setColumnWidth(2, 50);
    QTableView::resizeEvent(event);
}

QSize ErrorTable::sizeHint() const
{
    QSize size = QTableView::sizeHint();
    size.setHeight(_height);
    return size;
}

void ErrorTable::hideEvent(QHideEvent *)
{
    _height = height();
}
